home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / pass2.c < prev    next >
C/C++ Source or Header  |  1992-04-12  |  53KB  |  1,266 lines

  1. /*
  2.  *      pass2.c - cawf(1) pass 2 function
  3.  */
  4.  
  5. /*
  6.  *      Copyright (c) 1991 Purdue University Research Foundation,
  7.  *      West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *      University Computing Center.  Not derived from licensed software;
  11.  *      derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *      Permission is granted to anyone to use this software for any
  14.  *      purpose on any computer system, and to alter it and redistribute
  15.  *      it freely, subject to the following restrictions:
  16.  *
  17.  *      1. The author is not responsible for any consequences of use of
  18.  *         this software, even if they arise from flaws in it.
  19.  *
  20.  *      2. The origin of this software must not be misrepresented, either
  21.  *         by explicit claim or by omission.  Credits must appear in the
  22.  *         documentation.
  23.  *
  24.  *      3. Altered versions must be plainly marked as such, and must not
  25.  *         be misrepresented as being the original software.  Credits must
  26.  *         appear in the documentation.
  27.  *
  28.  *      4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32. #ifdef  UNIX
  33. #ifdef  USG
  34. #include <string.h>
  35. #else
  36. #include <strings.h>
  37. #endif
  38. #else
  39. #include <string.h>
  40. #endif
  41. #include <ctype.h>
  42.  
  43. /*
  44.  * Pass2(line) - process the nroff commands in a line and break
  45.  *               text into words for pass 3
  46.  */
  47.  
  48. void
  49. Pass2(line)
  50.         char *line;
  51. {
  52.         char buf[MAXLINE];              /* working buffer */
  53.         char c;                         /* character buffer */
  54.         double d;                       /* temporary double */
  55.         double exscale;                 /* expression scaling factor */
  56.         double expr[MAXEXP];            /* expressions */
  57.         char exsign[MAXEXP];            /* expression signs */
  58.         int i, j, k;                    /* temporary indexes */
  59.         int inword;                     /* word processing status */
  60.         int nexpr;                      /* number of expressions */
  61.         char nm[4];                     /* name */
  62.         int nsp;                        /* number of spaces */
  63.         char op;                        /* expression term operator */
  64.         char opstack[MAXSP];            /* expression operation stack */
  65.         char period;                    /* end of word status */
  66.         char *s1, *s2, *s3, *s4, *s5;   /* temporary string pointers */
  67.         double sexpr[MAXEXP];           /* signed expressions */
  68.         int sp;                         /* expression stack pointer */
  69.         char ssign;                     /* expression's starting sign */
  70.         int tabpos;                     /* tab position */
  71.         double tscale;                  /* term scaling factor */
  72.         double tval;                    /* term value */
  73.         double val;                     /* term value */
  74.         double valstack[MAXSP];         /* expression value stack */
  75.         char xbuf[MAXLINE];             /* expansion buffer */
  76.  
  77.         if (line == NULL) {
  78.     /*
  79.      * End of macro expansion.
  80.      */
  81.                 Pass3(DOBREAK, "need", NULL, 999);      /* flush page */
  82.                 return;
  83.         }
  84.     /*
  85.      * Adjust line number.
  86.      */
  87.         if (Lockil == 0)
  88.                 P2il++;
  89.     /*
  90.      * Empty line - "^[ \t]*$".
  91.      */
  92.         if (regexec(Pat[6].pat, line)) {
  93.                 Pass3(DOBREAK, "space", NULL, 0);
  94.                 return;
  95.         }
  96.     /*
  97.      * Line begins with white space.
  98.      */
  99.         if (*line == ' ' || *line == '\t') {
  100.                 Pass3(DOBREAK, "flush", NULL, 0);
  101.                 Pass3(0, "", NULL, 0);
  102.         }
  103.     /*
  104.      * Line contains text (not an nroff command).
  105.      */
  106.         if (*line != '.') {
  107.                 if (Font[0] == 'R' && Backc == 0 && Aftnxt == NULL
  108.                 &&  regexec(Pat[7].pat, line) == 0) {
  109.                     /*
  110.                      * The font is Roman, there is no "\\c" or "after next"
  111.                      * trap pending and and the line has no '\\', '\t', '-',
  112.                      * or "  "  (regular expression "\\|\t|-|  ").
  113.                      *
  114.                      * Output each word of the line as "<length> <word>".
  115.                      */
  116.                         for (s1 = line;;) {
  117.                                 while (*s1 && *s1 == ' ')
  118.                                         s1++;
  119.                                 if (*s1 == '\0')
  120.                                         break;
  121.                                 for (s2 = s1, s3 = buf; *s2 && *s2 != ' ';)
  122.                                         *s3++ = Trtbl[(int)*s2++];
  123.                                 *s3 = '\0';
  124.                                 Pass3((s2 - s1), buf, NULL, 0);
  125.                                 s1 = *s2 ? ++s2 : s2;
  126.                         }
  127.                     /*
  128.                      * Line terminates with punctuation and optional
  129.                      * bracketing (regular expression "[.!?:][\])'\"*]*$").
  130.                      */
  131.                         if (regexec(Pat[8].pat, line))
  132.                                 Pass3(NOBREAK, "gap", NULL, 2);
  133.                         if (Centering > 0) {
  134.                                 Pass3(DOBREAK,"center", NULL, 0);
  135.                                 Centering--;
  136.                         } else if (Fill == 0)
  137.                                 Pass3(DOBREAK, "flush", NULL, 0);
  138.                         return;
  139.                 }
  140.             /*
  141.              * Line must be scanned a character at a time.
  142.              */
  143.                 inword = nsp = tabpos = 0;
  144.                 period = '\0';
  145.                 for (s1 = line;; s1++) {
  146.                     /*
  147.                      * Space or TAB causes state transition.
  148.                      */
  149.                         if (*s1 == '\0' || *s1 == ' ' || *s1 == '\t') {
  150.                                 if (inword) {
  151.                                         if (!Backc) {
  152.                                                 Word[Wordx] = '\0';
  153.                                                 Pass3(Wordl, Word, NULL, 0);
  154.                                                 if (Uhyph) {
  155.                                                     Pass3(NOBREAK, "nohyphen",
  156.                                                         NULL, 0);
  157.                                                 }
  158.                                         }
  159.                                         inword = 0;
  160.                                         nsp = 0;
  161.                                 }
  162.                                 if (*s1 == '\0')
  163.                                         break;
  164.                         } else {
  165.                                 if (inword == 0) {
  166.                                         if (Backc == 0) {
  167.                                                 Wordl = Wordx = 0;
  168.                                                 Uhyph = 0;
  169.                                         }
  170.                                         Backc = 0;
  171.                                         inword = 1;
  172.                                         if (nsp > 1) {
  173.                                                 Pass3(NOBREAK, "gap", NULL,
  174.                                                     nsp);
  175.                                         }
  176.                                 }
  177.                         }
  178.                     /*
  179.                      * Process a character.
  180.                      */
  181.                         switch (*s1) {
  182.                     /*
  183.                      * Space
  184.                      */
  185.                         case ' ':
  186.                                 nsp++;
  187.                                 period = '\0';
  188.                                 break;
  189.                     /*
  190.                      * TAB
  191.                      */
  192.                         case '\t':
  193.                                 tabpos++;
  194.                                 if (tabpos <= Ntabs) {
  195.                                         Pass3(NOBREAK, "tabto", NULL,
  196.                                             Tabs[tabpos-1]);
  197.                                 }
  198.                                 nsp = 0;
  199.                                 period = '\0';
  200.                                 break;
  201.